package ufabc.edu.helppo.rpc.command.impl;

import static ufabc.edu.helppo.rpc.RPCOpenSimMethods.ADMIN_CREATE_USER;
import static ufabc.edu.helppo.rpc.RPCOpenSimParameters.*;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.HashMap;
import java.util.List;
import java.util.ResourceBundle;

import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.parser.XmlRpcResponseParser;

import ufabc.edu.helppo.gerador.exceptions.NenhumEspacoDisponivelException;
import ufabc.edu.helppo.rpc.command.RPCCommand;
import ufabc.edu.helppo.rpc.singleton.HelppoProperties;
import ufabc.edu.helppo.rpc.singleton.RpcClientSingleton;

public class CriaUsuarioRPCCommandImpl extends RPCCommand {
	
	private String firstname;
	private String lastname;
	private String password;
	private String email = "";

	@Override
	public boolean execute() throws MalformedURLException, XmlRpcException, IOException, NenhumEspacoDisponivelException {
		XmlRpcClient client = RpcClientSingleton.getInstance();
		configure(client);
		HashMap<String, String> retorno = (HashMap<String, String>) client.execute(ADMIN_CREATE_USER, parameter());

		return true;//Boolean.parseBoolean(retorno.get("success"));
	}

	
	//alterei para <String, Object> pq alguns parametros tem q ser string outros tem que ser int
	private List<Hashtable<String, Object>> parameter() throws IOException,
			NenhumEspacoDisponivelException {
		Hashtable<String, Object> parameters = new Hashtable<String, Object>();

		ResourceBundle properties = HelppoProperties.getInstance();

		parameters.put(USER_FIRSTNAME, getFirstname());
		parameters.put(USER_LASTNAME, getLastname());
		parameters.put(USER_PASSWORD, getPassword());
		parameters.put(START_REGION_X, 9500); //regiao inicial
		parameters.put(START_REGION_Y, 9500); //regiao inicial
		parameters.put(USER_EMAIL, getEmail());
		parameters.put(SERVER_PASSWORD, properties.getString(SERVER_PASSWORD));

		List<Hashtable<String, Object>> list = new ArrayList<Hashtable<String, Object>>();
		list.add(parameters);

		return list;
	}

	public String getFirstname() {
		return firstname;
	}

	public void setFirstname(String firstname_) {
		firstname = firstname_;
	}

	public String getLastname() {
		return lastname;
	}

	public void setLastname(String lastname_) {
		lastname = lastname_;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password_) {
		password = password_;
	}
	
	public void setEmail(String email_) {
		email = email_;
	}
	public String getEmail() {
		return email;
	}

}
